package Action;

import Entities.UserEntity;
import Service.RegisterService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.Map;

//控制注册的Action
public class RegisterAction extends ActionSupport
{
    private String user_name;
    private String user_password;
    private RegisterService registerService;
    private String[] tagNames;
    private String tag_register;

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_password() {
        return user_password;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public RegisterService getRegisterService() {
        return registerService;
    }

    public void setRegisterService(RegisterService registerService) {
        this.registerService = registerService;
    }

    public String[] getTagNames() {
        return tagNames;
    }

    public void setTagNames(String[] tagNames) {
        this.tagNames = tagNames;
    }

    public String getTag_register() {
        return tag_register;
    }

    public void setTag_register(String tag_register) {
        this.tag_register = tag_register;
    }

    //判断注册是否成功
    public String execute()
    {
        Map<String,Object> session =  ActionContext.getContext().getSession();
        session.remove("userNameOrPassword");
        boolean flag = false;
        //这个if和else-if中的内容可以在客户端判断
        if(user_name == null || user_name.trim().equals(""))
        {
            this.addFieldError("user_name","用户名不能为空");
            return "fail";
        }
        else if(user_password == null || user_password.trim().equals(""))
        {
            this.addFieldError("user_password","密码不能为空");
            return "fail";
        }
        else
        {
            flag = registerService.canRegister(user_name,user_password,2);
            if(flag == false)
            {
                session.put("userNameOrPassword","该用户名已被注册");
                return "fail";
            }
            else  //可以注册
            {
                //把用户名存入session
                session.put("user_name", user_name);
//                session.put("user_password", user_password);   //其实应该加密后再传
                session.put("isLogin",true);
                session.put("sessionid", ServletActionContext.getRequest().getSession().getId());
                UserEntity ue = new UserEntity();
                ue.setUserName(user_name);
                ue.setUserPassword(user_password);
                ue.setUserTag1("文学");  //此时默认用户有且仅有一个标签：文学
                ue.setTagMark1(0);
                ue.setTagMark2(0);
                ue.setTagMark3(0);
                ue.setTagMark4(0);
                ue.setTagMark5(0);
                ue.setTagMark6(0);
                registerService.addUser(ue);

                setTag_register("1");
                return "success";
            }
        }
    }

}
